//
//  ProgressHud.m
//  powercloudlefu
//
//  Created by Peng on 2018/5/29.
//  Copyright © 2018年 Peng. All rights reserved.
//

#import "ProgressHud.h"

@interface ProgressHud()

@property (nonatomic, strong) MBProgressHUD *mbHud;
@end;

@implementation ProgressHud


+ (instancetype)hud{
    static ProgressHud *hud = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        hud = [[ProgressHud alloc] init];
        hud.mbHud = [[MBProgressHUD alloc] init];
    });
    return hud;
}


+ (void)showText:(NSString *)text {
    if (text) {
        dispatch_async(dispatch_get_main_queue(), ^{
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].delegate.window animated:YES];
            hud.mode = MBProgressHUDModeText;
            hud.label.text = text;
            NSInteger length = text.length;
            CGFloat time = length * 0.15 < 1? 1:length * 0.15;
            [hud hideAnimated:YES afterDelay:time];
        });
    }
}

+ (void)dimBackgroundExample:(void(^)(MBProgressHUD *hud))dismissHandle {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[self topWindow] animated:YES];
    hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.backgroundView.color = [UIColor colorWithWhite:0.f alpha:0.1f];
    dismissHandle(hud);
}

+ (void)showDimBackGround{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[self topWindow] animated:YES];
    hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.backgroundView.color = [UIColor colorWithWhite:0.f alpha:0.1f];
}

+ (void)dismiddDimBackground{
    for (UIView *view in [self topWindow].subviews ) {
        if ([view isKindOfClass:[MBProgressHUD class]]) {
            [view removeFromSuperview];
        }
    }
}


+ (void)showHUDAddedTo:(UIView *)view animated:(BOOL)animated{
    [MBProgressHUD showHUDAddedTo:view animated:YES];
}

+ (void)hideHUDForView:(UIView *)view animated:(BOOL)animated{
    [MBProgressHUD hideHUDForView:view animated:YES];

}

+ (void)show{
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD showHUDAddedTo:[self topWindow] animated:YES];
    });
}

+ (void)hide{
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:[self topWindow] animated:YES];
    });
}

+ (MBProgressHUD *)showHUDWithText:(NSString *)text hideDelay:(NSTimeInterval)delay
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[self topWindow] animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = text;
    hud.removeFromSuperViewOnHide = YES;
    hud.label.numberOfLines=3;
    [hud hideAnimated:YES afterDelay:delay];
    return hud;
}

+ (MBProgressHUD *)showHUDWithText:(NSString *)text hideDelay:(NSTimeInterval)delay inView:(UIView *)view
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = text;
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:delay];
    return hud;
}


+ (UIWindow *)topWindow
{
    UIWindow *topWindow = nil;
    NSArray <UIWindow *>*windows = [UIApplication sharedApplication].windows;
    for (UIWindow *window in windows.reverseObjectEnumerator) {
        if (window.hidden == YES || window.opaque == NO) {
            // 隐藏的 或者 透明的 跳过
            continue;
        }
        if (CGRectEqualToRect(window.bounds, [UIScreen mainScreen].bounds) == NO) {
            // 不是全屏的 跳过
            continue;
        }
        topWindow = window;
        break;
    }
    return topWindow?:[UIApplication sharedApplication].delegate.window;
}
@end
